package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.comon.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    //分页查询
    @GetMapping("/page")
    public ResultInfo<Page<Dish>> findPage(Integer page, Integer pageSize, String name) {
        Page<Dish> dishPage = dishService.findPage(page, pageSize, name);
        return ResultInfo.success(dishPage);
    }

    //添加菜品
    @PostMapping
    @CacheEvict(value = "dish", allEntries = true)
    public ResultInfo<Object> saveDish(@RequestBody Dish dish) {
        dishService.saveDish(dish);
        return ResultInfo.success(null);
    }

    //根据id查询
    @GetMapping("/{id}")
    public ResultInfo<Dish> findById(@PathVariable("id") Long id) {
        Dish dish = dishService.getById(id);
        //根据dish的id查询对应口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper);
        //将结果封装到dish中
        if (CollectionUtil.isNotEmpty(dishFlavors)) {
            dish.setFlavors(dishFlavors);
        }
        return ResultInfo.success(dish);
    }

    //修改
    @PutMapping
    @CacheEvict(value = "dish", allEntries = true)
    public ResultInfo<Object> updateById(@RequestBody Dish dish) {
        dishService.updateDish(dish);
        return ResultInfo.success(null);
    }

    //删除
    @DeleteMapping
    @CacheEvict(value = "dish", allEntries = true)
    public ResultInfo<Object> deleteByIds(@RequestParam("ids") List<Long> ids) {
        dishService.deleteByIds(ids);
        return ResultInfo.success(null);
    }

    //停售与启售
    @PostMapping("/status/{value}")
    @CacheEvict(value = "dish", allEntries = true)
    public ResultInfo<Object> updateStatus(@PathVariable("value") Integer status, @RequestParam List<Long> ids) {
        //接收status
        Dish dish = new Dish();
        dish.setStatus(status);
        //修改状态
        for (Long id : ids) {
            dish.setId(id);
            dishService.updateById(dish);
        }
        return ResultInfo.success(null);
    }

    //查询套餐里的菜品
    @GetMapping("/list")
    public ResultInfo<List<Dish>> findDish(Long categoryId) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);
        List<Dish> dishList = dishService.list(wrapper);
        return ResultInfo.success(dishList);
    }
}
